//
//  ZJShaixuanCell.m
//  这家测试项目
//
//  Created by IOS_DEVELOPER on 2017/7/5.
//  Copyright © 2017年 陈志凌. All rights reserved.
//

#import "ZJShaixuanCell.h"

#import "ZJOrientationModel.h"
#import "ZJHouse_layoutModel.h"
#import "ZJArea_circleMdel.h"

@implementation ZJShaixuanCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    _houseLarr = [NSMutableArray new];
    _areaArr = [NSMutableArray new];
    _oriArr = [NSMutableArray new];
    
    self.mainImg.sd_layout.leftSpaceToView(self.contentView, 8).topSpaceToView(self.contentView, 5).heightIs(30).widthIs(30);
    self.mainTitle.sd_layout.leftSpaceToView(self.mainImg, 8).topSpaceToView(self.contentView, 10).rightSpaceToView(self.contentView, 20).autoHeightRatio(0);
    self.bottomView.sd_layout.leftSpaceToView(self.contentView, 8).topSpaceToView(self.mainImg, 10).rightSpaceToView(self.contentView, 20).autoHeightRatio(0);
}

-(void)setSxArr:(NSArray *)sxArr{
    _sxArr = sxArr;
}

-(void)setModelArr:(NSArray *)modelArr{
    _modelArr = modelArr;
    
    float width = (self.contentView.width - 80)/3;
    float height = 32;
    float space = 20;
    
  
        NSObject * model = _modelArr[0];

        if ([model.class isSubclassOfClass:[ZJHouse_layoutModel class]]) {
            self.tag = 0;
        }else if ([model.class isSubclassOfClass:[ZJArea_circleMdel class]]){
            self.tag = 1;
        }else if ([model.class isSubclassOfClass:[ZJOrientationModel class]]){
            self.tag = 2;
        }

    
    switch (self.tag) {
        case 0:
        {
            self.mainImg.image = [UIImage imageNamed:@"sx_icon_gj"];
            self.mainTitle.text = @"格局";
            NSArray *subViews = [self.bottomView subviews];
            
            if([subViews count] != 0) {
                
                [subViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
                
                [_houseLarr removeAllObjects];
            }
            for (int i = 0; i < _modelArr.count; i++) {
                UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(i % 3 * (width + space), i / 3 * (height + 12), width, height)];
                
                ZJHouse_layoutModel * Fmodel = _modelArr[i];
                [btn setTitle:Fmodel.house_layout forState:0];
                [btn setBackgroundColor:[UIColor whiteColor]];
                
                [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
                [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
                
                btn.titleLabel.textAlignment = NSTextAlignmentCenter;
                [btn setTitleColor:[UIColor grayColor] forState:0];
                float fontSize = [Utils iphone5AutoScaleW:10.0f];
                btn.titleLabel.font = [UIFont systemFontOfSize:fontSize];
                btn.tag = i;
                [btn addTarget:self action:@selector(clickHouse_layoutBtn:) forControlEvents:(UIControlEventTouchUpInside)];
                
                btn.layer.borderColor = RGB(235, 235, 235).CGColor;
                btn.layer.borderWidth = 0.5f;
                [self.bottomView addSubview:btn];
                
                if ([self.sxArr containsObject:Fmodel.house_layout]) {
                    [btn setBackgroundColor:[Utils colorWithHexString:allConstHexColor]];
                    [btn setTitleColor:[UIColor whiteColor] forState:0];
                    btn.selected = YES;
                    self.houseBtn = btn;
                    if (_delegate != nil && [_delegate respondsToSelector:@selector(clickHouse_layoutBtn:)]) {
                        
                        [_delegate clickHouse_layoutBtn:btn.titleLabel.text];
                    }
                }
                
                [_houseLarr addObject:btn];
   
            }
            [self.bottomView setupAutoHeightWithBottomViewsArray:_houseLarr bottomMargin:0];
            [self setupAutoHeightWithBottomView:self.bottomView bottomMargin:8];
        }
            break;
            
        case 1:
        {
            self.mainImg.image = [UIImage imageNamed:@"sx_icon_mj"];
            self.mainTitle.text = @"面积";
            NSArray *subViews = [self.bottomView subviews];
            
            if([subViews count] != 0) {
                
                [subViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
                
                [_areaArr removeAllObjects];
            }
            for (int i = 0; i < _modelArr.count; i++) {
                UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(i % 3 * (width + space), i / 3 * (height + 12), width, height)];
                
                ZJArea_circleMdel * Fmodel = _modelArr[i];
                [btn setTitle:Fmodel.area_circle forState:0];
                [btn setBackgroundColor:[UIColor whiteColor]];
                
                [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
                [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
                
                btn.titleLabel.textAlignment = NSTextAlignmentCenter;
                [btn setTitleColor:[UIColor grayColor] forState:0];
                btn.titleLabel.font = [UIFont systemFontOfSize:[Utils iphone5AutoScaleW:10.0f]];
                btn.tag = i;
                [btn addTarget:self action:@selector(clickArea_circleBtn:) forControlEvents:(UIControlEventTouchUpInside)];
                
                btn.layer.masksToBounds = YES;
                btn.layer.borderColor = RGB(235, 235, 235).CGColor;
                btn.layer.borderWidth = 0.5f;
                [self.bottomView addSubview:btn];
                
                if ([self.sxArr containsObject:Fmodel.area_circle]) {
                    [btn setBackgroundColor:[Utils colorWithHexString:allConstHexColor]];
                    [btn setTitleColor:[UIColor whiteColor] forState:0];
                    btn.selected = YES;
                    self.areaBtn = btn;
                    if (_delegate != nil && [_delegate respondsToSelector:@selector(clickArea_circleBtn:)]) {
                        
                        [_delegate clickArea_circleBtn:btn.titleLabel.text];
                    }
                }
                
                [_areaArr addObject:btn];
                
            }
            [self.bottomView setupAutoHeightWithBottomViewsArray:_areaArr bottomMargin:0];
            [self setupAutoHeightWithBottomView:self.bottomView bottomMargin:8];
        }
            break;
            
        case 2:
        {
            self.mainImg.image = [UIImage imageNamed:@"sx_icon_cx"];
            self.mainTitle.text = @"朝向";
            NSArray *subViews = [self.bottomView subviews];
            
            if([subViews count] != 0) {
                
                [subViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
                
                [_oriArr removeAllObjects];
            }
            for (int i = 0; i < _modelArr.count; i++) {
                UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(i % 3 * (width + space), i / 3 * (height + 12), width, height)];
                
                ZJOrientationModel * Fmodel = _modelArr[i];
                [btn setTitle:Fmodel.orientation forState:0];
                [btn setBackgroundColor:[UIColor whiteColor]];
                
                [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
                [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
                
                btn.titleLabel.textAlignment = NSTextAlignmentCenter;
                [btn setTitleColor:[UIColor grayColor] forState:0];
                btn.titleLabel.font = [UIFont systemFontOfSize:[Utils iphone5AutoScaleW:10.0f]];
                btn.tag = i;
                [btn addTarget:self action:@selector(clickOrientationBtn:) forControlEvents:(UIControlEventTouchUpInside)];
                
                btn.layer.masksToBounds = YES;
                btn.layer.borderColor = RGB(235, 235, 235).CGColor;
                btn.layer.borderWidth = 0.5f;
                [self.bottomView addSubview:btn];
                
                if ([self.sxArr containsObject:Fmodel.orientation]) {
                    [btn setBackgroundColor:[Utils colorWithHexString:allConstHexColor]];
                    [btn setTitleColor:[UIColor whiteColor] forState:0];
                    btn.selected = YES;
                    self.oriBtn = btn;
                    if (_delegate != nil && [_delegate respondsToSelector:@selector(clickOrientationBtn:)]) {
                        
                        [_delegate clickOrientationBtn:btn.titleLabel.text];
                    }
                }
                
                [_oriArr addObject:btn];
                
            }
            [self.bottomView setupAutoHeightWithBottomViewsArray:_oriArr bottomMargin:0];
            [self setupAutoHeightWithBottomView:self.bottomView bottomMargin:8];
        }
            break;
            
        default:
            break;
    }
}

-(void)clickHouse_layoutBtn:(UIButton *)sender{
    
    if (!sender.selected) {
        
        if (self.houseBtn) {
            self.houseBtn.selected = NO;
            [self.houseBtn setBackgroundColor:[UIColor whiteColor]];
            [self.houseBtn setTitleColor:[UIColor grayColor] forState:0];
        }
        sender.selected = NO;
        UIButton * btn =_houseLarr[sender.tag];
        [btn setBackgroundColor:[UIColor whiteColor]];
        [sender setBackgroundColor:[Utils colorWithHexString:allConstHexColor]];
        [sender setTitleColor:[UIColor whiteColor] forState:0];
        btn.selected = YES;
        
        self.houseBtn = sender;
        
        if (_delegate != nil && [_delegate respondsToSelector:@selector(clickHouse_layoutBtn:)]) {

            [_delegate clickHouse_layoutBtn:sender.titleLabel.text];
        }
        
    }else{
    
        self.houseBtn.selected = NO;
        [self.houseBtn setBackgroundColor:[UIColor whiteColor]];
        [self.houseBtn setTitleColor:[UIColor grayColor] forState:0];
        
        sender = self.houseBtn;
        
        if (_delegate != nil && [_delegate respondsToSelector:@selector(clickHouse_layoutBtn:)]) {
            
            [_delegate clickHouse_layoutBtn:nil];
        }
    }
}

-(void)clickArea_circleBtn:(UIButton *)sender{
    if (!sender.selected) {
        
        if (self.areaBtn) {
            self.areaBtn.selected = NO;
            [self.areaBtn setBackgroundColor:[UIColor whiteColor]];
            [self.areaBtn setTitleColor:[UIColor grayColor] forState:0];
        }
        sender.selected = NO;
        UIButton * btn =_areaArr[sender.tag];
        [btn setBackgroundColor:[UIColor whiteColor]];
        [sender setBackgroundColor:[Utils colorWithHexString:allConstHexColor]];
        [sender setTitleColor:[UIColor whiteColor] forState:0];
        btn.selected = YES;
        
        self.areaBtn = sender;
        
        if (_delegate != nil && [_delegate respondsToSelector:@selector(clickArea_circleBtn:)]) {
            
            [_delegate clickArea_circleBtn:sender.titleLabel.text];
        }
        
    }else{
        
        self.areaBtn.selected = NO;
        [self.areaBtn setBackgroundColor:[UIColor whiteColor]];
        [self.areaBtn setTitleColor:[UIColor grayColor] forState:0];
        
        sender = self.areaBtn;
        
        if (_delegate != nil && [_delegate respondsToSelector:@selector(clickArea_circleBtn:)]) {
            
            [_delegate clickArea_circleBtn:nil];
        }
    }
}

-(void)clickOrientationBtn:(UIButton *)sender{
    
    if (!sender.selected) {
        
        if (self.oriBtn) {
            self.oriBtn.selected = NO;
            [self.oriBtn setBackgroundColor:[UIColor whiteColor]];
            [self.oriBtn setTitleColor:[UIColor grayColor] forState:0];
        }
        sender.selected = NO;
        UIButton * btn =_oriArr[sender.tag];
        [btn setBackgroundColor:[UIColor whiteColor]];
        [sender setBackgroundColor:[Utils colorWithHexString:allConstHexColor]];
        [sender setTitleColor:[UIColor whiteColor] forState:0];
        btn.selected = YES;
        
        self.oriBtn = sender;
        
        if (_delegate != nil && [_delegate respondsToSelector:@selector(clickOrientationBtn:)]) {
            
            [_delegate clickOrientationBtn:sender.titleLabel.text];
        }
        
    }else{
        
        self.oriBtn.selected = NO;
        [self.oriBtn setBackgroundColor:[UIColor whiteColor]];
        [self.oriBtn setTitleColor:[UIColor grayColor] forState:0];
        
        sender = self.oriBtn;
        
        if (_delegate != nil && [_delegate respondsToSelector:@selector(clickOrientationBtn:)]) {
            
            [_delegate clickOrientationBtn:nil];
        }
    }
}

@end
